<?php

/*
 *	字符串截取 Class
 *	author	yanxf <walkfine@gmail.com>
 *	version	mocube_3.0
 */

class StringComponent{
	//字符截取
	public static function cutStr($sourceStr, $cutLength = 10, $extStr = '...'){
		$returnStr = ''; $i = 0; $n = 0;
		$strLength = strlen($sourceStr);
		if ($strLength > $cutLength){
			while (($n < $cutLength) and ($i <= $strLength)){
				$tempStr = substr($sourceStr, $i, 1);
				$ascnum = Ord($tempStr);
				if ($ascnum >= 224){
					$returnStr = $returnStr . substr($sourceStr, $i, 3);
					$i = $i+3;
					$n++;
				}elseif ($ascnum >= 192){
					$returnStr=$returnStr.substr($sourceStr,$i,2);
					$i=$i+2;
					$n++;
				}elseif ($ascnum >= 65 && $ascnum <= 90){
					$returnStr = $returnStr.substr($sourceStr, $i, 1);
					$i = $i+1;
					$n++;
				}else{
					$returnStr = $returnStr.substr($sourceStr, $i, 1);
					$i = $i+1;
					$n = $n+0.5;
				}
			}
			if ($strLength > $i){
				$returnStr = $returnStr.$extStr;
			}
			return $returnStr;
		}
		else
			return $sourceStr;
	}

	public static function replaceStr($string, $start, $length = 0, $str = '*')
	{
		if ($start > mb_strlen($string, 'utf-8')) {
			return $string;
		} else if ($start + $length > mb_strlen($string, 'utf-8')) {
			$length = mb_strlen($string, 'utf-8') - $start;
		} else if ($length == 0) {
			$length = mb_strlen($string, 'utf-8') - $start;
		}
		$replace = '';
		for ($i = 0; $i < $length; $i++) {
			$replace .= $str;
		}
		$result = mb_substr($string, 0, $start -mb_strlen($string, 'utf-8'), 'utf-8') . $replace . mb_substr($string, $start + $length, -1, 'utf-8');
		return $result;
	}

	public static function hiddenName($name)
	{
		$result = StringComponent::replaceStr($name, 1);
		return $result;
	}

	public static function hiddenMobile($mobile)
	{
		$result = StringComponent::replaceStr($mobile, 3, 4);
		return $result;
	}
}
?>
